package com.ngm.tea.t.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ngm.tea.common.PageInfo;
import com.ngm.tea.t.dao.ModuleDao;
import com.ngm.tea.t.dao.PrivilegeDao;
import com.ngm.tea.t.dao.RolePrivilegeDao;
import com.ngm.tea.t.entity.Module;
import com.ngm.tea.t.entity.Privilege;
import com.ngm.tea.t.entity.Tree;
import com.ngm.tea.t.service.ModuleService;

/**
 * 服务层
 * 
 * @author zjw
 * @version 1.0
 * @date 2016-09-01 21:07:07
 *
 */
@Service("moduleService")
public class ModuleServiceImpl implements ModuleService {

	@Resource
	private ModuleDao moduleDao;
	@Resource
	private PrivilegeDao privilegeDao;
	@Resource
	private RolePrivilegeDao rolePrivilegeDao;

	public ModuleDao getModuleDao() {
		return moduleDao;
	}

	@Override
	public void insert(Module module) {
		moduleDao.insert(module);
	}

	@Override
	public void deleteById(Long id) {
		moduleDao.deleteById(id);
	}

	@Override
	public void deleteByIds(String ids) {
		String[] _ids = ids.split(",");
		if (_ids.length > 0) {
			List<String> idlist = Arrays.asList(_ids);
			moduleDao.deleteByIds(idlist);
		}
	}

	@Override
	public void delete(Module module) {
		moduleDao.delete(module);
	}

	@Override
	public void update(Module module) {
		moduleDao.update(module);
	}

	@Override
	public Module selectOne(Long id) {
		return moduleDao.selectOne(id);
	}

	@Override
	public List<Module> selectAll() {
		return moduleDao.selectAll();
	}

	@Override
	public List<Module> selectList(Module module, PageInfo pageInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("searchParams", makeStr(module));
		map.put("pageInfo", pageInfo);
		map.put("order", pageInfo.getOrderField() + " " + pageInfo.getOrderDirection());
		return moduleDao.selectList(map);
	}

	@Override
	public Long count(Module module) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("searchParams", makeStr(module));
		return moduleDao.count(map);
	}

	public String makeStr(Module module) {
		StringBuffer sb = new StringBuffer();
		if (!StringUtils.isEmpty(module.getId())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("id LIKE '%" + module.getId() + "%'");
			} else {
				sb.append(" AND id LIKE '%" + module.getId() + "%'");
			}
		}
		if (!StringUtils.isEmpty(module.getModCode())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("modCode LIKE '%" + module.getModCode() + "%'");
			} else {
				sb.append(" AND modCode LIKE '%" + module.getModCode() + "%'");
			}
		}
		if (!StringUtils.isEmpty(module.getModName())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("modName LIKE '%" + module.getModName() + "%'");
			} else {
				sb.append(" AND modName LIKE '%" + module.getModName() + "%'");
			}
		}
		if (!StringUtils.isEmpty(module.getParentCode())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("parentCode LIKE '%" + module.getParentCode() + "%'");
			} else {
				sb.append(" AND parentCode LIKE '%" + module.getParentCode() + "%'");
			}
		}
		if (!StringUtils.isEmpty(module.getModUrl())) {
			if (StringUtils.isEmpty(sb.toString())) {
				sb.append("modUrl LIKE '%" + module.getModUrl() + "%'");
			} else {
				sb.append(" AND modUrl LIKE '%" + module.getModUrl() + "%'");
			}
		}
		if (StringUtils.isEmpty(sb.toString())) {
			return null;
		} else {
			return sb.toString();
		}
	}

	@Override
	public Module selectOneByCode(String modCode) {
		return moduleDao.selectOneByCode(modCode);
	}

	@Override
	public List<Module> selectOneByParentCode(String parentCode) {
		return moduleDao.selectOneByParentCode(parentCode);
	}

	public String creatTree(Tree tree, Module module) throws Exception {
		tree.setName(module.getModName());
		tree.setClick("link(this)");
		tree.setTarget("t/module/children.do?code=" + module.getModCode());
		tree.setOpen(true);
		List<Module> list = moduleDao.selectOneByParentCode(module.getModCode());
		for (Module mod : list) {
			Tree children = new Tree();
			tree.children.add(children);
			creatTree(children, mod);
		}
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(tree);
		return json;
	}

	List<Privilege> pris;

	private List<Privilege> getPris() {
		if (pris == null) {
			pris = privilegeDao.selectAll();
		}
		return pris;
	}
	
	public String creatTreeP(Tree tree, Module module, List<Map<String,Object>> maps) throws Exception {
		String modCode = module.getModCode();
		tree.setTarget(modCode);
		tree.setName(module.getModName());
		if (module.getModLevel() > 1) {
			for (Privilege pri : getPris()) {
				String priCode = pri.getPriCode();
				Tree children = new Tree();
				children.setTarget(priCode);
				children.setName(pri.getPriName());
				for(Map<String,Object> rp:maps) {
					String _modCode = (String) rp.get("mod_code");
					String _priCode = (String) rp.get("pri_code");
					if(modCode.equals(_modCode)&&priCode.equals(_priCode)) {
						children.setChecked(true);
					}
				}
				tree.children.add(children);
			}
		}
		tree.setOpen(true);
		List<Module> list = moduleDao.selectOneByParentCode(module.getModCode());
		for (Module mod : list) {
			Tree children = new Tree();
			tree.children.add(children);
			creatTreeP(children, mod, maps);
		}
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(tree);
		return json;
	}
	
	
	public List<Module> selectMenu(String accCode) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("accCode", accCode);
		List<Module> list = moduleDao.selectMenu(map);
		List<Module> list2 = new ArrayList<>();
		for(Module mod:list) {
			selectPMenu(mod,list2);
		}
		list2.addAll(list);
		System.out.println(list2.size());
		return list2;
	}
	
	private List<Module> selectPMenu(Module mod,List<Module> list) {
		Module module = this.selectOneByCode(mod.getParentCode());
		if(module !=null) {
			list.remove(module);
			list.add(module);
			selectPMenu(module,list);
		}
		return list;
	}
	
	
//	@Override
//	public Tree selectMenu(String accCode) throws Exception {
//		Map<String, String> map = new HashMap<String, String>();
//		map.put("accCode", accCode);
//		List<Map<String, String>> list = moduleDao.selectMenu(map);
//		List<String> slist = new ArrayList<>();
//		for (int i = 0; i < list.size(); i++) {
//			String mod_code = list.get(i).get("mod_code");
//			slist.add(mod_code);
//		}
//		Module module = this.selectOneByCode("rear_manage");
//		Tree tree = new Tree();
//		tree.setName(module.getModName());
//		tree.setOpen(true);
//		List<Module> mods = moduleDao.selectOneByParentCode(module.getModCode());
//		for (Module mod : mods) {
//			Tree children = new Tree();
//			Tree _tree = this.creatMenu(children, mod, slist);
//			if(_tree.children.size() > 0) {
//				tree.children.add(_tree);
//			}
//		}
//		return tree;
//	}
	
//	public Tree creatMenu(Tree tree, Module module,List<String> list) throws Exception {
//		tree.setName(module.getModName());
//		tree.setTarget(module.getModUrl());
//		tree.setClick("link(this)");
//		tree.setOpen(true);
//		List<Module> mods = moduleDao.selectOneByParentCode(module.getModCode());
//		for (Module mod : mods) {
//			Tree children = new Tree();
//			if(list.contains(mod.getModCode())) {
//				tree.children.add(children);
//			}
//			creatMenu(children, mod, list);
//		}
//		return tree;
//	}
	
	public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
		Tree tree = new Tree();
		tree.setName("a");
		Tree tree2 = new Tree();
		tree2.setName("a2");
		Tree tree3 = new Tree();
		tree3.setName("a3");
		tree.children.add(tree2);
		tree.children.add(tree3);
		tree.children.remove(tree3);
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(tree);
		System.out.println(json);
	}
}